package com.ellricky.teacher.ui.mycourse.evaluate

import com.andr.common.http.callback.JOCallback
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.http.Api
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.base.BaseTakePhotoActivity
import com.ellricky.baselib.listener.WorkUploadListener
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.photo.TakePhotoUtil
import com.ellricky.teacher.API_STU_EVALUATE
import com.ellricky.teacher.R
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.tc_activity_works_upload.*
import org.devio.takephoto.model.TResult
import org.json.JSONObject
import java.io.File
import java.util.*

class WorkUploadActivity : BaseTakePhotoActivity(), WorkUploadListener {

    private lateinit var customerId: String
    private lateinit var attendId: String
    private lateinit var classStatus: String
    private lateinit var nlFirst: String
    private lateinit var nlSecond: String
    private lateinit var nlThird: String
    private lateinit var nlFour: String
    private lateinit var nlFive: String

    private var curPicPosition: Int = 0

    override fun getContentLayout() = R.layout.tc_activity_works_upload


    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.tc_work_upload)

        customerId = intent.getStringExtra("customerId") ?: ""
        attendId = intent.getStringExtra("attendId") ?: ""
        classStatus = intent.getStringExtra("classStatus") ?: ""
        nlFirst = intent.getStringExtra("nlFirst") ?: ""
        nlSecond = intent.getStringExtra("nlSecond") ?: ""
        nlThird = intent.getStringExtra("nlThird") ?: ""
        nlFour = intent.getStringExtra("nlFour") ?: ""
        nlFive = intent.getStringExtra("nlFive") ?: ""

        workUploadView.setListener(this)
        workUploadView.updateAdapterData("", 0)

        btnSubmit.setOnClickListener lit@{
            postUpload()
        }

    }

    private fun postUpload() {

        val fileUrs = workUploadView.urls
        if (EmptyUtil.isEmpty(fileUrs)) {
            ToastUtil.show(this, "请添加作品")
            return
        }

        /* val title = etTitle.text.toString().trim()
         if (EmptyUtil.isEmpty(title)) {
             ToastUtil.show(this, "请添加标题")
             return
         }*/

        val record = etContent.text.toString().trim()
        if (EmptyUtil.isEmpty(record)) {
            ToastUtil.show(this, "请添加课堂记录")
            return
        }

        // 上传
        val url = EAppUtil.getApiHost() + API_STU_EVALUATE
        val paramMap = mapOf(
                "customerId" to customerId,
                "attendId" to attendId,
                "classStatus" to classStatus,
                "teacherComment" to record,
                "nlFirst" to nlFirst,
                "nlSecond" to nlSecond,
                "nlThird" to nlThird,
                "nlFour" to nlFour,
                "nlFive" to nlFour,
                "fileUrls" to fileUrs
        )
        HttpUtils.getInstance().post(url, paramMap, object : JOCallback(this) {
            override fun onSuccess(dataJO: JSONObject, msg: String) {
                setResult(BaseConstant.RESULT_CODE_ADD)
                finish()
            }
        })

    }


    override fun takePhoto(position: Int, optionItem: Int) {
        curPicPosition = position

        createImageUri()
        TakePhotoUtil.configTakePhotoOption(takePhoto)
        TakePhotoUtil.configCompress(takePhoto)

        if (optionItem == 0) {
            // 拍照
            TakePhotoUtil.takeCamera(takePhoto, null, imageUri)
        } else if (optionItem == 1) {
            TakePhotoUtil.takePicOne(takePhoto, null, imageUri)
        }

    }


    override fun takeSuccess(result: TResult?) {
        val localPath = result?.image?.compressPath ?: ""
        Logger.t("detail").e("%s", localPath)

        // 上传服务器，转换成url
        val url = EAppUtil.getApiHost() + Api.API_UPLOAD_ACT_FILE
        val file = File(localPath)
        val paramMap = HashMap<String, String>()
        HttpUtils.getInstance().uploadPhoto(url, file, paramMap, object : JOCallback(this) {
            override fun onSuccess(dataJO: JSONObject, msg: String) {
                val realDataJo = dataJO.optJSONObject("data")
                if (realDataJo != null) {
                    val fileUrl = realDataJo.optString("url")
                    if (!EmptyUtil.isEmpty(fileUrl)) {
                        workUploadView.updateAdapterData(fileUrl, curPicPosition)
                    }
                }

            }
        })
    }
}